define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/offlinepay/index',
                    // add_url: 'pro/offlinepay/add',
                    edit_url: 'pro/offlinepay/edit',
                    // del_url: 'pro/offlinepay/del',
                    multi_url: 'pro/offlinepay/multi',
                    table: 'gzns_offline_pay',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    if (!params.filter) {
                        params.filter={}
                    }else {
                        params.filter = JSON.parse(params.filter)
                    }
                    if (!params.op) {
                        params.op={}
                    }else {
                        params.op = JSON.parse(params.op)

                    }
                    if (!params.filter['order.order_type'] && !params.op['order.order_type']) {
                        params.filter['order.order_type'] = '1';
                        params.op['order.order_type'] = '='
                    }

                    params.filter = JSON.stringify(params.filter)
                    params.op = JSON.stringify(params.op)

                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},

                        {field: 'order.order_number', title: __('Order.order_number'),operate:'Like'},
                        {field: 'order.item.vipapply.company_name', title: __('Company_name'),operate:'Like'},
                        {field: 'path', title: __('Path'), formatter: Table.api.formatter.image,operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'order.id', title: __('Order.id')},
                        // {field: 'order.user_id', title: __('Order.user_id')},
                        // {field: 'order.order_status', title: __('Order.order_status')},
                        // {field: 'order.complete_time', title: __('Order.complete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'order.close_time', title: __('Order.close_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'order.pay_time', title: __('Order.pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'order.pay_type', title: __('Order.pay_type'), searchList: {"0":__('Pay_type 0'),"1":__('Pay_type 1'),"2":__('Pay_type 2'),"3":__('Pay_type 3')}, formatter: Table.api.formatter.status},
                        // {field: 'order.user_message', title: __('Order.user_message')},
                        // {field: 'order.order_type', title: __('Order.order_type')},
                        // {field: 'order.appoint_time', title: __('Order.appoint_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'order.cancel_reason', title: __('Order.cancel_reason')},
                        {field: 'order.total_price', title: __('Order.total_price'), operate:'BETWEEN'},
                        {field: 'order.actual_price', title: __('Order.actual_price'), operate:'BETWEEN'},
                        // {field: 'order.refused_price', title: __('Order.refused_price'), operate:'BETWEEN'},
                        // {field: 'order.cancel_type', title: __('Order.cancel_type')},
                        // {field: 'order.wx_card_code', title: __('Order.wx_card_code')},
                        // {field: 'order.ticket_code_path', title: __('Order.ticket_code_path')},
                        // {field: 'order.user_vip_apply', title: __('Order.user_vip_apply')},
                        // {field: 'order.master_id', title: __('Order.master_id')},
                        // {field: 'order.serial_number', title: __('Order.serial_number')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit,.btn-add").data("area", ["100%", "100%"]);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $('[data-status]').click(function () {
                var status = $(this).attr('data-status')
                $('#c-status').val(status)
                var tip = '确认吗'
                if(status == 3){
                    tip = '设为无效后将自动关闭订单，确认吗？'
                } else if(status == 2){
                    tip = '确认收款后订单将自动完成，确认吗？'
                }
                Layer.confirm(tip, {icon: 3, title:'提示'}, function(index){
                    //do something
                    $("form[role=form]").submit()
                    Layer.close(index);
                });
                //
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});